﻿<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Web.Services.Description;
using System.Web.SessionState;


public class UploadHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";

            HttpPostedFile file = context.Request.Files["Filedata"];
            string uploadPath =
                HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

            if (file != null)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string filename = Guid.NewGuid().ToString() + "." + file.FileName.Split(new char[] {'.'})[1];
                string filePath = uploadPath +filename;
                file.SaveAs(filePath);
                //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                context.Response.Write(filename);
            }
            else
            {
                context.Response.Write("0");
            }  
        }
        catch (Exception ex)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(ex.Message);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}